<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class ShareGoodRecord_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_share_record_by_id($id){
        $query = $this->db
            ->select('*')
            ->get_where('goods_share_record', array('id'=>$id));
        return $query->row_array();
    }

    public function get_share_record_by_uid($uid){
        $query = $this->db
            ->select('*')
            ->get_where('goods_share_record', array('user_id'=>$uid));
        return $query->row_array();
    }

    /**
     * 根据条件查询
     * @param $where array
     * @return mixed
     */
    public function get_share_record_by($uid){
        $query = $this->db
            ->select('goods_share_record.*,shop_goods.good_title,shop_goods.good_cover')
            ->from('goods_share_record')
            ->join('shop_goods','shop_goods.good_id=goods_share_record.goods_id','LEFT')
            ->where('goods_share_record.user_id', $uid)
            ->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改分享记录信息
     */
    public function sava_share_record($id,$data){

        $this->db->where('id',$id);
        $this->db->update('goods_share_record', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增分享记录
     */
    public function add_share_record($data){

        $this->db->insert('goods_share_record',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有分享记录
     */
    public function get_all_goods_share_record(){
        $this->db->select('*');
        $this->db->from('goods_share_record');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }


    /**
     * 获取启用的规则
     */
    public function get_goods_share_record_by($map = "status=1"){
        $sql = "SELECT * FROM {$this->db->dbprefix}goods_share_record  where $map ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除分享记录
     */
    public function delete_goods_share_record_by($where){
        $this->db->where($where)->delete('goods_share_record');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function get_share_record_limit($uid,$good_id){
        $query = $this->db
            ->select('goods_share_record.*')
            ->from('goods_share_record')
            ->where('goods_share_record.user_id', $uid)
            ->where('goods_share_record.goods_id', $good_id)
            ->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
    }

    public function set_int_click_count($id){
        $this->db->set('share_click_count','share_click_count+1',false)->where('id',$id)->update('goods_share_record');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

}
